<template>
  <div>
    <div v-if="kindergartens.length>1">
      <div class="weui-mask"></div>
      <div class="weui-dialog">
        <div class="weui-dialog__hd"><strong class="weui-dialog__title">请选择需要登陆的幼儿园</strong></div>
        <div class="weui-cells__group weui-cells__group_form">
          <div class="weui-cells weui-cells_radio">
            <div class="weui-cell weui-check__label" for="x11" v-for="item in kindergartens"
                 @click="changeSelect(item.id)">
              <div class="weui-cell__bd">
                <p><input type="radio" name="radio"
                          :value="item.id" v-model="kindergartenId" id="x11">{{item.name}} </p>
              </div>
            </div>
          </div>
        </div>
        <div class="weui-dialog__ft" @click="changeKindergarten">
          <a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_primary">确定</a>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import wx from "weixin-js-sdk";

  export default {
    name: "temp",
    components: {},
    data() {
      return {
        kindergartens: [],
        kindergartenId: '',
        openId: '',
        auth: {
          code: "",
          kindergartenId: ""
        }
      };
    },
    methods: {
      changeSelect(id) {
        this.kindergartenId = id;
      },
      check() {
        let selectType = this.auth.loginType;
        this.$http.checkUserBind(this.auth).then(resp => {
          let data = resp.data;
          let openId=data.openId;
          if (!openId){
            wx.closeWindow();
            return;
          }
          this.$Cookies.set("indexurl",window.location.href.split('#')[0]);
          this.$Cookies.set("token", data.token);
          this.$Cookies.set("fullName", data.fullName);
          this.$Cookies.set("loginType", data.loginType);
          this.$Cookies.set("headImage", data.headImage);
          this.$Cookies.set("custom", data.custom);
          this.$Cookies.set("openId", data.openId);
          if (selectType === 'kinsfolk') {
            if (data.custom) {
              this.$router.push('/kindergarten-list');
              return;
            }
            let kindergartenList = data.kindergartenList;
            if (kindergartenList.length > 1) {
              this.kindergartens = kindergartenList;
              return;
            }
            this.$router.push("/home");
          }else if(selectType==='personal'){
            //判断是否是个人用户
            //判断是否注册
            if (data.custom) {
              //跳转个人注册页
              this.$router.push('/per-regis');
              return;
            }
            //查询是否绑定过学生
            this.$http.fetchPersonalList().then(res=>{
              //绑定了就跳转学生列表界面
              if(res.data&&res.data.length>0){
                this.$router.push("/per-bind");
              }
              //未绑定跳转增加学生页面
              else{
                this.$router.push("/per-regi");
              }
            })
          }else {
            //未绑定
            if (!data.bind) {
              let r = confirm("您尚未绑定微信！是否前往绑定？");
              if (r) {
                this.$router.push({
                  path: "/bind",
                  query: {
                    openId: data.openId,
                    loginType: this.auth.loginType,
                    kindergartenId: this.auth.kindergartenId
                  }
                });
              } else {
                wx.closeWindow();
              }
            } else {
              //已绑定
              this.$router.push("/home");
            }
          }
        });
      },
      changeKindergarten() {
        this.$http.loginInKindergarten({id: this.kindergartenId}).then(resp => {
          this.$router.push("/home");
        })
      }
    },
    created() {
      this.auth.code = this.$route.query.code;
      this.auth.loginType = this.$route.query.selectType;
    },
    mounted() {
      this.check();
    }
  };
</script>

<style scoped>
</style>
